module.exports = function($scope,$resource,$state,$modalInstance,$window){
    $scope.currentid ;
    $scope.currentcode ;
    $scope.currentname ;
//    $resource('/a/sys/office/treeData', {}, {}).query({}, function(res){
//          $scope.currentid = res[0].id;
//          $scope.currentcode = res[0].code;
//          $scope.currentname = res[0].name;
//          $scope.code = $scope.currentcode;
//          $scope.obj['company_id'] = $scope.currentid;
//          $scope.obj['office_id'] = $scope.currentid;
//    });    
    
   console.log($scope.info)
    $scope.company = {};
    $scope.obj = {'company_id': $scope.info.company_id};
    $scope.obj.loginName = '';
    $scope.obj.name = '';
    var idobj = {};
   // $scope.reloadRoute = function () {
   // 	$window.location.reload();
   // };
   $scope.cancel = function () {
               $modalInstance.dismiss('cancel');              
               //$scope.reloadRoute();
   };
   $resource('/api/as/sc/role/getForUserList', {}, {}).save({'company_id':'e460ff8cf25847cabf89bda6e6a3002c'}, function(res){
       $scope.objs = res.data;
       $scope.company = res.company;
       console.log('!@#!@#@!#!@#@!');
       console.log(res);
   });
   $scope.ok = function(){

       
       if($scope.obj.loginName === '')
       {
           alert('用户名必填');
           return;
       }

       if($scope.obj.name === '')
       {
           alert('姓名必填');
           return;
       }

       $scope.obj.rolelist = [];
       angular.forEach(idobj, function (value, key) {
           $scope.obj.rolelist.push(key)
       });

       if($scope.obj.rolelist.length === 0)
       {
           alert('请选择角色');
           return;
       }
       var reg = /^[u4E00-u9FA5]+$/; 
       if(!reg.test($scope.info.company_name+$scope.obj.loginName)){ 
           alert('登录账号不能有中文');
       return ; 
       } 
       $scope.obj.login_name = $scope.info.company_code+$scope.obj.loginName;
       $scope.obj.no = $scope.obj.loginName;
       $scope.obj.loginName = $scope.obj.loginName;
       $scope.obj.newPassword = '000000';
       $scope.obj.confirmNewPassword = '000000';
       $scope.obj.loginFlag = '1';
       var savedata={};
       savedata.name=$scope.obj.name;
       savedata.mobile=$scope.obj.mobile;
       savedata.remark=$scope.obj.remark;
       savedata.loginname=$scope.info.company_code+$scope.obj.loginName;
       
       $resource('/api/ac/sc/systemUserService/addUserForEquipment', {}, {}).save($scope.obj,function(res){
          if(res.errcode==0){                  
                   $resource('/api/ac/ggc/equipmentPerManageService/create', {}, {}).save(savedata, function(res){
                       if(res.errcode==0){
                           if(confirm("是否继续添加")){
                               $scope.obj.name="";
                               $scope.obj.mobile="";
                               $scope.obj.remark="";
                               $scope.obj.loginName="";                     
                           }else{
                                 $modalInstance.dismiss('cancel');
                                 //$scope.reloadRoute();	
                           }				
                       }else{
                           alert(res.errmsg);
                       }
                   });
          }else{
                   alert(res.errmsg);
               }

   //         $modalInstance.close(officeid, officename);

       });

   };


   
   $scope.selection = function($event, obj){


       var checkbox = $event.target;
       
       if(checkbox.checked)
       {
           idobj[obj.id] = '';
       }
       else
       {
          delete idobj[obj.id];
       }

   };
   //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   //  $scope.equipmentperson={};
   // //用户名非空验证
   // $scope.equipmentperson.nameadd=function(){
   // $scope.equipmentperson.namecheck=false;
   // }
   // $scope.equipmentperson.nameblur=function(){
   // 	if ($scope.equipmentperson.name==undefined || $scope.equipmentperson.name==""){
   // 				$scope.equipmentperson.namecheck=true;
   // 			}
   // }
   // //维护周期非空验证
   // $scope.equipmentperson.mobileadd=function(){	
   // $scope.equipmentperson.mobilecheck=false;
   // }
   // $scope.equipmentperson.mobileblur=function(){
   // 	if ($scope.equipmentperson.mobile==undefined || $scope.equipmentperson.mobile==""){
   // 				$scope.equipmentperson.mobilecheck=true;
   // 			}
   // }
   // $scope.reloadRoute = function () {
   // 	$window.location.reload();
   // };
   // $scope.cancel = function () {
   //             $modalInstance.dismiss('cancel');
   // 			$scope.reloadRoute();
   //         };
   // $scope.ok=function(){
   // 	//提交校验
   //    if($scope.equipmentperson.name==undefined || $scope.equipmentperson.name==""){
   // 		$scope.equipmentperson.namecheck=true;
   // 		return;
   //    }
   //    if($scope.equipmentperson.mobile==undefined || $scope.equipmentperson.mobile==""){
   // 		$scope.equipmentperson.mobilecheck=true;
   // 		return;
   //    }	   								
   // 	var savedata={};
   // 	savedata.name=$scope.equipmentperson.name;
   // 	savedata.mobile=$scope.equipmentperson.mobile;
   //  savedata.remark=$scope.equipmentperson.remark;
   // 	$resource('/api/ac/ggc/equipmentPerManageService/create', {}, {}).save(savedata, function(res){
   // 		if(res.errcode==0){
   // 			if(confirm("是否继续添加")){
   //                  $scope.equipmentperson={};                     
   // 			}else{
   // 				  $modalInstance.dismiss('cancel');
   // 				  $scope.reloadRoute();	
   // 			}				
   // 		}else{
   // 			alert(res.errmsg);
   // 		}
   // 	});			   
   // 	};	
};